//
//  SLVideoCell.m
//  Alltelevisionnews
//
//  Created by dllo on 15/11/30.
//  Copyright © 2015年 三人行. All rights reserved.
//

#import "SLVideoCell.h"
#import "SLVideoCellModel.h"
#import <MediaPlayer/MediaPlayer.h>
#import <Masonry.h>

@interface SLVideoCell ()


@end

@implementation SLVideoCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = [UIColor colorWithRed:246 / 255.0 green:247 / 255.0 blue:249 / 255.0 alpha:1];
        [self createSubviews];
        [self createConstrain];
        NSLog(@"cell init");
    }
    
    return self;
}


+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *identifier = @"video";
    SLVideoCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (! cell) {
        cell = [[SLVideoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    return cell;
}


- (void)setModel:(SLVideoCellModel *)model
{
    if (_model != model) {
        _model = model;
    }
    
    self.titleLabel.text = model.title;
    self.descriptionLabel.text = model.myDescription;
    [self.playImageView sd_setImageWithURL:[NSURL URLWithString:model.cover] placeholderImage:nil];
    self.videoDurationLabel.text = [self convertVideoDurationTime:model.length];
    [self.videoDurationLabel sizeToFit];
    self.playCountLabel.text = [self convertPlayCount:model.playCount];
    [self.playCountLabel sizeToFit];
    self.playTimeLabel.text = [model.ptime substringWithRange:NSMakeRange(5, 11)];
}


- (NSString *)convertVideoDurationTime:(NSString *)timeLength;
{
    NSInteger totalTime = timeLength.integerValue;
    NSInteger sec = totalTime % 60;
    NSInteger min = totalTime / 60;
    
    NSString *timeStr = [NSString stringWithFormat:@"%02ld:%02ld", min, sec];
    
    return timeStr;
}


- (NSString *)convertPlayCount:(NSNumber *)playCountStr
{
    NSInteger playCountNum = playCountStr.integerValue;
    
    NSString *convertStr = (playCountNum > 9999)? [NSString stringWithFormat:@"%.1f万", playCountNum / 1000.0] : playCountStr.stringValue;
   
    return convertStr;
}


- (void)createSubviews
{
    self.backView = [[UIView alloc] init];
    [self.contentView addSubview:self.backView];
    
    self.titleLabel = [[UILabel alloc] init];
    [self.contentView addSubview:self.titleLabel];
    
    self.descriptionLabel = [[UILabel alloc] init];
    [self.contentView addSubview:self.descriptionLabel];
    self.descriptionLabel.font = [UIFont systemFontOfSize:15];
    self.descriptionLabel.textColor = [UIColor grayColor];
    
    self.playImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:self.playImageView];
   
    self.videoDurationImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:self.videoDurationImageView];
    self.videoDurationImageView.image = [UIImage imageNamed:@"videoDuration.png"];
    
    self.videoDurationLabel = [[UILabel alloc] init];
    [self.contentView addSubview:self.videoDurationLabel];
    self.videoDurationLabel.font = [UIFont systemFontOfSize:13];
    self.videoDurationLabel.textColor = [UIColor grayColor];
    
    self.playCountImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:self.playCountImageView];
    self.playCountImageView.image = [UIImage imageNamed:@"playCount.png"];
    
    self.playCountLabel = [[UILabel alloc] init];
    [self.contentView addSubview:self.playCountLabel];
    self.playCountLabel.font = [UIFont systemFontOfSize:13];
    self.playCountLabel.textColor = [UIColor grayColor];
    
    self.playButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:self.playButton];
    [self.playButton setBackgroundImage:[UIImage imageNamed:@"playicon"] forState:UIControlStateNormal];
        
    self.playTimeLabel = [[UILabel alloc] init];
    [self.contentView addSubview:self.playTimeLabel];
    self.playTimeLabel.font = [UIFont systemFontOfSize:13];
    self.playTimeLabel.textColor = [UIColor grayColor];
    
//    // 收藏按钮
//    self.videoCollectionButton = [[UIButton alloc] init];
//    [self.contentView addSubview:self.videoCollectionButton];
    
    if ([ZDQSingleton shareSingleton].isLongin == YES) {
        self.contentView.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.3 alpha:1];
        self.titleLabel.textColor = [UIColor whiteColor];
        self.descriptionLabel.textColor = [UIColor whiteColor];
        self.playImageView.alpha = 0.4;
        self.videoDurationImageView.alpha = 0.4;
        self.videoDurationLabel.textColor = [UIColor whiteColor];
        self.playTimeLabel.textColor = [UIColor whiteColor];
        self.playCountLabel.textColor = [UIColor whiteColor];
         self.backView.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.3 alpha:1];
    } else {
        
        self.contentView.backgroundColor = [UIColor colorWithRed:246 / 255.0 green:247 / 255.0 blue:249 / 255.0 alpha:1];
        self.titleLabel.textColor = [UIColor blackColor];
        self.descriptionLabel.textColor = [UIColor grayColor];
        self.videoDurationLabel.textColor = [UIColor grayColor];
        self.playImageView.alpha = 1;
        self.videoDurationImageView.alpha = 1;
        self.playTimeLabel.textColor = [UIColor grayColor];
        self.playCountLabel.textColor = [UIColor grayColor];
         self.backView.backgroundColor = [UIColor whiteColor];
    }
}


- (void)createConstrain
{
    CGFloat gapX = 10;
    CGFloat gapY = 10;
    
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView).insets(UIEdgeInsetsMake(8, 0, 0, 0));
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(gapX);
        make.top.equalTo(self.contentView).offset(gapY * 2);
        make.right.equalTo(self.contentView).offset(-gapX);
        make.height.mas_equalTo(30);
    }];
    
    [self.descriptionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom);
        make.left.equalTo(self.contentView).offset(gapX);
        make.right.equalTo(self.contentView).offset(-gapX);
        make.height.mas_equalTo(25);
    }];
    
    [self.playImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.descriptionLabel.mas_bottom).offset(10);
        make.left.equalTo(self.contentView).offset(gapX);
        make.right.equalTo(self.contentView).offset(-gapX);
        make.height.equalTo(self.playImageView.mas_width).multipliedBy(9.0 / 16.0);
    }];
    
    [self.videoDurationImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(gapX);
        make.top.equalTo(self.playImageView.mas_bottom).offset(gapY * 2);
        make.width.and.height.mas_equalTo(22);
    }];
    
    [self.videoDurationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.videoDurationImageView);
        make.left.equalTo(self.videoDurationImageView.mas_right).offset(4);
    }];
    
    [self.playCountImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.videoDurationImageView);
        make.left.equalTo(self.videoDurationLabel.mas_right).offset(25);
        make.width.and.height.equalTo(self.videoDurationImageView);
    }];
    
    [self.playCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.videoDurationImageView);
        make.left.equalTo(self.playCountImageView.mas_right).offset(4);
    }];
    
    [self.playButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.playImageView);
        make.width.and.height.mas_equalTo(60);
    }];
    
    [self.playTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-gapX);
        make.centerY.equalTo(self.videoDurationImageView);
    }];
}



@end
